package com.itheima.tanhua.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.tanhua.api.db.UserServiceApi;
import com.itheima.tanhua.mapper.UserInfoMapper;
import com.itheima.tanhua.mapper.UserMapper;
import com.itheima.tanhua.pojo.db.UserInfo;
import com.itheima.tanhua.pojo.db.user;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.Year;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 芝士雪豹
 * @create 2022/9/19/14:19
 */
@DubboService //发送到注册中心
public class UserServiceApiImpl implements UserServiceApi {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfomapper;


    @Override
    public user findByPhone(String phone) {
        LambdaQueryWrapper<user> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user::getMobile, phone);
        user user = userMapper.selectOne(queryWrapper);
        //List<UserInfo> userInfos = userInfomapper.selectBatchIds(list);
        //Map<Long, UserInfo> map = new HashMap<>();
        //userInfos.forEach(u -> map.put(u.getId(), u));
        //return map;
        return user;
    }

    @Override
    public Long save(user user1) {
        userMapper.insert(user1);
        return user1.getId();
    }

    @Override
    public synchronized void addUserInfo(UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getId, userInfo.getId());
        Integer num = userInfomapper.selectCount(queryWrapper);
        if (num == 0) {
            userInfomapper.insert(userInfo);
        } else {
            userInfomapper.updateById(userInfo);
        }
    }

    //根据id查询用户信息
    @Override
    public UserInfo findById(Long userId) {
        return userInfomapper.selectById(userId);
    }

    //更新环信 用户信息
    @Override
    public void update(user user) {
        userMapper.updateById(user);
    }

    @Override
    public user findByUserId(Long userId) {
        user user = userMapper.selectById(userId);
        return user;
    }

    //根据环信id查询用户基本信息
    @Override
    public user findByHxId(String huanxinId) {
        LambdaQueryWrapper<user> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user::getHxUser, huanxinId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id", userIds);
        //2、添加筛选条件
        if (info != null) {
            if (info.getAge() != null) {
                qw.lt("age", info.getAge());
            }
            if (!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender", info.getGender());
            }
            if (!StringUtils.isEmpty(info.getNickname())) {
                qw.like("nickname", info.getNickname());
            }
        }
        List<UserInfo> list = userInfomapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }
}
